package com.base.cn.platform.os.controller.web.classes.info;

import com.base.cn.platform.os.common.WebBaseController;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.entity.classes.topic.type.ClassesTopicType;
import com.base.cn.platform.os.service.course.classes.ClassesService;
import com.base.cn.platform.os.service.course.classes.member.ClassesMemberService;
import com.base.cn.platform.os.service.course.classes.topic.ClassesTopicService;
import com.base.cn.platform.os.service.course.classes.topic.type.ClassesTopicTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 班级详情
 *
 * @author SK
 * @since 2018-05-28
 */
@Controller
@RequestMapping("/web/classes")
public class WebClassesInfoController extends WebBaseController {

    @Autowired
    private ClassesService classesService;
    @Autowired
    private ClassesMemberService classesMemberService;
    @Autowired
    private ClassesTopicService classesTopicService;
    @Autowired
    private ClassesTopicTypeService  classesTopicTypeService;

    /**
     * 班级详情
     *
     * @param id 班级id
     * @return 班级详情页
     */
    @RequestMapping("/info")
    public String info(Model model,
                       HttpServletRequest request,
                       @RequestParam("id") BigDecimal id) {
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        //获取当前班级访问路径(用于购买课程成功后的回调)
        String requestURL = request.getRequestURL().toString();
        requestURL += "?id="+id;
        Map<String, Object> classes = classesService.findClassesById(id, false, false, true, false, 0);
        model.addAttribute("requestURL",requestURL);
        model.addAttribute("classes", classes);
        model.addAttribute("login", DataUtil.idIsNotNull(userId));
        //判断是否加入了班级，true是，false否
        boolean joined = classesMemberService.joined(id, userId);
        model.addAttribute("joined", joined);
        //话题分类统计
        List<Map<String, Object>> classesTypeNum = classesTopicTypeService.getClassesTypeNum(id,null);
        model.addAttribute("topicTypeNum",classesTypeNum);
        return resultPage("/classes/info/classes-info");
    }
}
